package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.BasePedigreeDao;
import com.linktel.productionLine.dao.BaseStepDao;
import com.linktel.productionLine.dao.StepYieldOffsetDao;
import com.linktel.productionLine.entity.BasePedigree;
import com.linktel.productionLine.entity.BaseStep;
import com.linktel.productionLine.entity.ProductYieldOffset;
import com.linktel.productionLine.entity.StepYieldOffset;
import com.linktel.productionLine.entity.dto.stepYieldOffset.StepYieldOffsetDTO;
import com.linktel.productionLine.entity.query.stepYieldOffset.AddStepYieldOffsetQuery;
import com.linktel.productionLine.entity.query.stepYieldOffset.EditStepYieldOffsetQuery;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author HuangJiaHui
 * @Date 2023/9/19 10:09
 */
@Service
@AllArgsConstructor
public class StepYieldOffsetService {
    private StepYieldOffsetDao stepYieldOffsetDao;
    private BasePedigreeDao basePedigreeDao;
    private BaseStepDao baseStepDao;

    /**
     * 查询所有工序良率补偿
     *
     * @return 良率补偿列表
     */
    public List<StepYieldOffsetDTO> listStepYieldOffset() {
        return stepYieldOffsetDao.listStepYieldOffset();
    }

    /**
     * 新增工序良率补偿
     * 系列编码和产品编码只能有一条对应数据
     *
     * @param addStepYieldOffsetQuery 新增的工序良率补偿封装对象
     * @return 是否新增成功
     */
    public Boolean addStepYieldOffset(AddStepYieldOffsetQuery addStepYieldOffsetQuery) {
        String productSeriesCode = addStepYieldOffsetQuery.getProductSeriesCode();
        String productCode = addStepYieldOffsetQuery.getProductCode();
        String stepCode = addStepYieldOffsetQuery.getStepCode();

        StepYieldOffset stepYieldOffset = new StepYieldOffset();
        BeanUtils.copyProperties(addStepYieldOffsetQuery, stepYieldOffset);
        if (stepCode == null || "".equals(stepCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "工序编码不能为空");
        }
        if ((productSeriesCode == null || "".equals(productSeriesCode))) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产品系列编码是必填项");
        }
        BasePedigree basePedigree1 = basePedigreeDao.getByCode(productSeriesCode);
        stepYieldOffset.setProductSeriesId(basePedigree1.getId());
        stepYieldOffset.setProductSeriesName(basePedigree1.getName());
        if (productCode != null && !"".equals(productCode)) {
            BasePedigree basePedigree2 = basePedigreeDao.getByCode(productCode);
            stepYieldOffset.setProductId(basePedigree2.getId());
            stepYieldOffset.setProductName(basePedigree2.getName());
        }
        List<Long> result = stepYieldOffsetDao.getByProductCode(productSeriesCode, productCode, stepCode);
        if (!CollectionUtils.isEmpty(result)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "已经存在该产品编码和工序对应的工序良率补偿，不能重复添加");
        }
        BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCode);
        stepYieldOffset.setStepId(baseStep.getId());
        stepYieldOffset.setStepName(baseStep.getName());
        return stepYieldOffsetDao.addStepYieldOffset(stepYieldOffset, addStepYieldOffsetQuery.getCurrentUserId());
    }

    /**
     * 修改工序良率补偿
     *
     * @param editStepYieldOffsetQuery 要修改的工序良率补偿
     * @return 是否修改成功
     */
    @Transactional
    public Boolean editStepYieldOffset(EditStepYieldOffsetQuery editStepYieldOffsetQuery) {
        Long id = editStepYieldOffsetQuery.getId();
        String productSeriesCode = editStepYieldOffsetQuery.getProductSeriesCode();
        String productCode = editStepYieldOffsetQuery.getProductCode();
        String stepCode = editStepYieldOffsetQuery.getStepCode();
        StepYieldOffset stepYieldOffset = new StepYieldOffset();
        BeanUtils.copyProperties(editStepYieldOffsetQuery, stepYieldOffset);
        if (stepCode == null || "".equals(stepCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "工序编码不能为空");
        }
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        if ((productSeriesCode == null || "".equals(productSeriesCode))) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "产品系列编码是必填项");
        }
        BasePedigree basePedigree1 = basePedigreeDao.getByCode(productSeriesCode);
        stepYieldOffset.setProductSeriesId(basePedigree1.getId());
        stepYieldOffset.setProductSeriesName(basePedigree1.getName());
        if (productCode != null && !"".equals(productCode)) {
            BasePedigree basePedigree2 = basePedigreeDao.getByCode(productCode);
            stepYieldOffset.setProductId(basePedigree2.getId());
            stepYieldOffset.setProductName(basePedigree2.getName());
        }
        BaseStep baseStep = baseStepDao.getBaseStepByCode(stepCode);
        stepYieldOffset.setStepId(baseStep.getId());
        stepYieldOffset.setStepName(baseStep.getName());

        stepYieldOffsetDao.editStepYieldOffset(stepYieldOffset, editStepYieldOffsetQuery.getCurrentUserId());

        List<Long> result = stepYieldOffsetDao.getByProductCode(productSeriesCode, productCode, stepCode);
        if (result.size() >= 2) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "已经存在该产品编码和工序对应的工序良率补偿，不能重复添加");
        }
        return true;
    }

    /**
     * 删除工序良率补偿
     *
     * @param currentUserId 当前登录用户id
     * @param id            删除的工序良率补偿id
     * @return 是否删除成功
     */
    public Boolean deleteStepYieldOffset(Long currentUserId, Long id) {
        return stepYieldOffsetDao.deleteStepYieldOffset(currentUserId, id);
    }
}
